#include "coin.h"
#include <QPixmap>
#include <QDebug>

Coin::Coin(QString img)
{
	QPixmap pix;
	bool ret = pix.load(img);
	if(!ret)
	{
		qDebug() << "图片加载失败";
		return;
	}
	this->setFixedSize(pix.width(),pix.width());
//	this->setStyleSheet("QPushbutton{border:0px;}");
	this->setIcon(pix);
	this->setIconSize(QSize(pix.width(),pix.height()));

	// 初始化金币翻转定时器
	timer1 = new QTimer(this);
	timer2 = new QTimer(this);

	// 监听正面翻反面定时器
	connect(timer1,&QTimer::timeout,[=](){
		QPixmap pix;
		QString str = QString(":/Coin000%1.png").arg(this->min++);
		pix.load(str);
		this->setIcon(pix);

		// 判断翻完，min重置位1
		if(this->min > this->max)
		{
			this->min = 1;
			isAnimation = false;
			timer1->stop();
		}
	});
	// 监听反面翻正面定时器
	connect(timer2,&QTimer::timeout,[=](){
		QPixmap pix;
		QString str = QString(":/Coin000%1.png").arg(this->max--);
		pix.load(str);
		this->setIcon(pix);

		// 判断翻完，min重置位1
		if(this->max < this->min)
		{
			this->max = 8;
			isAnimation = false;
			timer2->stop();
		}
	});
}
//重写按下事件，防止连续翻转，动画播放顺序打乱
void Coin::mousePressEvent(QMouseEvent *e)
{
	if(this->isAnimation)
	{
		return;
	}
	else
	{
		QPushButton::mousePressEvent(e);
	}
}
void Coin::change_flag()
{
	if(this->flag)
	{
		timer1->start(80);
		isAnimation = true;
		this->flag = false;
	}
	else
	{
		timer2->start(80);
		isAnimation = true;
		this->flag = true;
	}
}

